//进入页面加载数据
var index=1;//默认首页为1
var count; //总条数
var PageSize=10;//设置每页示数目
var PageCount;//计算总页数
$(function(){
	
	getProduct(1)
})
//获取表格
var content = $('.content');
function  getProduct(index){
	var alltr ='';
	$.ajax({
		type:"get",
		url:"/api/pro/v1/getlist.action",
		async:true,
		data:{index:parseInt(index)},
		success:function(data){

			var datalist = data.prolist.products;
			count = data.prolist.count;//总条数
			PageCount=Math.ceil(count/PageSize);//总页数
			content.empty();//首次清空
			$('.pageNew').text(index);
			$('.pageCount').text(PageCount);
			for(var i=0;i<datalist.length;i++){
				eachtr="<tr><td>"+
				datalist[i].productPid
				+"</td><td>"+
				datalist[i].productMoney
				+"</td><td>"+
				datalist[i].productDate
				+"</td><td>"+
				datalist[i].productInterest
				+"</td><td>"+
				datalist[i].productDeadline
				+"</td><td>"+
				datalist[i].productType
				+"</td><td>"+
				datalist[i].productLabel
				+"</td><td>"+
				datalist[i].productCreationTime
				+"</td><td><i class='mdi mdi-delete sa-warning' onclick=\'deleteProduct("+datalist[i].productPid+")\'></i><i class='fa fa-edit' onclick=\'x_admin_show(\"修改信息\",\"changeProduct.html?&productPid="+datalist[i].productPid+"&productMoney="+datalist[i].productMoney+"&productDate="+datalist[i].productDate+"&productInterest="+datalist[i].productInterest+"&productDeadline="+datalist[i].productDeadline+"&productType="+datalist[i].productType+"&productLabel="+datalist[i].productLabel+"\")\'></i></td></tr>"
				alltr+=eachtr;
				content.html(alltr);
			}
			
			
		}
	});
}




/*删除产品*/
function deleteProduct(id) {
		
	swal({
		title: "你确定要删除嘛?",
		type: "warning",
		showCancelButton: !0,
		confirmButtonClass: "btn btn-success mt-2",
		cancelButtonClass: "btn btn-danger ml-2 mt-2",
		confirmButtonText: "确定",
		cancelButtonText: "取消",
		buttonsStyling: !1
	}).then(function(t) {

		t.value === true ? $.post('/api/pro/v1/delpro.action',{
			pid: id
		}, function(data) {
			
			swal({
				title: "删除成功",
				text: "你的操作已生效",
				type: "success",
				confirmButtonClass: "btn btn-confirm mt-2"
			}).then(function() {
				var pageNew = $('.pageNew').text();
				if(data.code == 0) {
					getProduct(1);
					layer.msg(data.msg)
					
				}else{
					layer.msg(data.msg)
				}
			})

		}) : "cancel" === t.dismiss && swal({
			title: "操作取消",
			type: "error",
			timer: 2e3,
			confirmButtonClass: "btn btn-confirm mt-2"
		})

	})

}

//首页
$('.homePage').click(function(){
	getProduct(1);
})
//下一页
$('.nextPage').click(function(){
	index = index+1;
	if(index<=PageCount){
		getProduct(index);
	}else{
		getProduct(PageCount)
	}

})
//上一页
$('.prePage').click(function(){
	index-=1;
	if(index<=1){
		getProduct(1)
	}else{
		getProduct(index)
	}
	
})
//尾页
$('.lastpage').click(function(){
	getProduct(PageCount)
})



/*打开页面*/
function x_admin_show(title,url,w,h){
    if (title == null || title == '') {
        title=false;
    };
    if (url == null || url == '') {
        url="404.html";
    };
    if (w == null || w == '') {
        w=($(window).width()*0.9);
    };
    if (h == null || h == '') {
        h=($(window).height() + 500);
    };
    layer.open({
        type: 2,
        fix: false, //不固定
        maxmin: true,
        area: ['450px', '450px'],
        shadeClose: true,
        shade:0.4,
        title: title,
        content: url
    });
}

/*关闭弹出框口*/
function x_admin_close(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}